let isDev = process.env.NODE_ENV == 'development';

//src\router\index.js

import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

let arrRoute = [
  {
    path: '/',
    name: 'index',
    component: () => {
      return import('@/Views/index');
    },
    meta: {
      title: 'index',
    },
  },
  {
    path: '/HelloWorld',
    name: 'HelloWorld',
    component: () => {
      return import('@/components/HelloWorld/index');
    },
    meta: {
      title: 'hw',
    },
  },
];

if (isDev) {
  arrRoute.push(
    {
      path: '/test',
      name: 'HelloWorld',
      component: () => {
        return import('@/components/HelloWorld');
      },
    },
    {
      path: '/queryTablePage',
      name: 'queryTablePage',
      component: () => {
        return import('@/components/HelloWorld/queryTablePage.vue');
      },
    },
    {
      path: '/uploadQiNiu',
      name: 'uploadQiNiu',
      component: () => {
        return import('@/components/HelloWorld/uploadQiNiu.vue');
      },
    }
  );
}

let router = new Router({
  routes: arrRoute,
});

router.beforeEach((to, from, next) => {
  if (to.meta && to.meta.title) {
    document.title = to.meta.title;
  }
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    // 检查用户是否已登录
    if (!isAuthenticated()) {
      next({
        path: '/login',
        query: { redirect: to.fullPath },
      });
    } else {
      // 检查用户角色
      if (!to.meta.roles.includes(getUserRole())) {
        next({ path: '/403' });
      } else {
        next();
      }
    }
  } else {
    next(); // 确保一定要调用 next()
  }
});

export default router;

// router.beforeEach((to, from, next) => {
//   if (to.meta && to.meta.title) {
//     document.title = to.meta.title;
//   }
//   next();
// });
